
/*==========================================================================
	Name:		SetUsed
	Purpose: 	Disables the AutoRemove Timer on an entity
==========================================================================*/
local function SetUsed(Entity,Bool)
	if ( SERVER ) and ( Bool ) and ( Entity.WPP ) then
		Entity.WPP.Used = true
		
		umsg.Start("WPP_ES")
			
			umsg.Char(1)
			umsg.Entity(Entity)
			
			umsg.Char(15)
			umsg.Bool(true)
			
			umsg.Char(0)
			
		umsg.End()
	end
end

/*==========================================================================
	Name:		Tool
	Purpose: 	Can a player use this tool on that entity
==========================================================================*/
local function Tool(Player,Trace,Mode)
	local Key = "Tool"
	if ( !Trace.Entity ) or ( Trace.Entity:IsWorld() ) then return end
	if ( Mode == "duplicator" ) or ( Mode == "adv_duplicator" ) then Key = "Duplicate" end
	if ( Mode == "remover" ) then Mode = "Remove" end
	local B = Player:WPP_EntityCheck(Trace.Entity,Key)
	SetUsed(Trace.Entity,B)
	return B
end

/*==========================================================================
	Name:		Physgun
	Purpose: 	Can a player use their Physgun on that entity
==========================================================================*/
local function Physgun(Player,Entity)
	if ( Entity:IsPlayer() ) then return end
	local B = Player:WPP_EntityCheck(Entity,"Touch")
	SetUsed(Entity,B)
	return B
end

/*==========================================================================
	Name:		GravGun
	Purpose: 	Can a player use their GravGun on that entity
==========================================================================*/
local function GravGun(Player,Entity)
	local B = Player:WPP_EntityCheck(Entity,"Grab")
	SetUsed(Entity,B)
	return B
end

/*==========================================================================
	Name:		Use
	Purpose: 	Can a player use their Use key on that entity
==========================================================================*/
local function Use(Player,Entity)
	return Player:WPP_EntityCheck(Entity,"Use")
end

/*==========================================================================
	Name:		Damage
	Purpose: 	Can a player Damage that entity
==========================================================================*/
local function Damage(Entity,Player,Attacker,Ammount,Info)
	if ( GetConVarNumber("wpp_anti_damage") >= 1 ) and ( Player ) and ( Player:IsPlayer() ) then
		if ( !Player:WPP_EntityCheck(Entity,"Damage") ) then
			return true
		end
	end
end

/*==========================================================================
	Name:		Collide
	Purpose: 	Can an entity colide with that
==========================================================================*/
function Collide(A,B)
	local N = GetConVarNumber("wpp_anti_colide")
	if ( N > 1 ) then return end
	if ( !A.WPP ) or ( !B.WPP ) then return end
	if ( A.WPP.Collide == WPP_COLIDE_NORMAL ) and ( B.WPP.Collide == WPP_COLIDE_NORMAL ) then return end
	if ( A.WPP.Collide == WPP_COLIDE_SOLID ) or ( B.WPP.Collide == WPP_COLIDE_SOLID ) then return true end
	if ( A.WPP.Collide == WPP_COLIDE_GHOST ) or ( B.WPP.Collide == WPP_COLIDE_GHOST ) then return false end
end

/*==========================================================================
	Section:	Hooks
==========================================================================*/
hook.Add("CanTool","WPP",Tool)
hook.Add("CanPlayerUnfreeze","WPP",Physgun)
hook.Add("PhysgunPickup","WPP",Physgun)
hook.Add("GravGunPickupAllowed","WPP",GravGun)
hook.Add("GravGunPunt","WPP",GravGun)
hook.Add("PlayerUse","WPP",Use)
hook.Add("EntityTakeDamage","WPP",Damage)
hook.Add("ShouldCollide","WPP",Collide)

/*==========================================================================
	Hook:		PlayerInitialSpawn -> WPP
	Purpose: 	Sync A player with WPP!
==========================================================================*/
if ( SERVER ) then
	
	hook.Add("PlayerInitialSpawn","WPP",function(Player)
		WPP:SyncEntitys(ents.GetAll(), Player)
		WPP:SyncTrust(Player)
		timer.Destroy("WPP_" .. Player:UniqueID())
	end)
	
/*==========================================================================
	Hook:		PlayerDisconnected -> WPP
	Purpose: 	Sync A player with WPP!
==========================================================================*/
	hook.Add("PlayerDisconnected","WPP",function(Player)
		local Time = GetConVarNumber("wpp_auto_clean")
		local Rank = GetConVarNumber("wpp_auto_admins")
		
		if ( Time == 0 ) then return end
		if ( Player:IsAdmin() ) and ( Rank == 1 ) then return end
		if ( Player:IsSuperAdmin() ) and ( Rank == 2 ) then return end
		
		timer.Create("WPP_" .. Player:UniqueID(),Time,1,function(Uid,Nick)
			for _,Entity in pairs ( ents.GetAll() ) do
			
				if ( Entity:WPP_IsOwned() ) and ( Entity:WPP_UniqueID() == Uid ) then
					Entity:Remove()
				end
				
			end
			
			PrintMessage(HUD_PRINTNOTIFY, Nick .. " props have been cleaned up.")
		end,Player:UniqueID(),Player:Nick())
		
		PrintMessage(HUD_PRINTNOTIFY, Player:Nick() .. " props will be cleaned up in " .. Time .. " seconds.")
	end)

/*==========================================================================
	Timer:		WPP_AR
	Purpose: 	Checks if it should auto remove any props
==========================================================================*/

	timer.Create("WPP_AR",1,0,function()
		if ( GetConVarNumber("wpp_auto_remove") > 0 ) then
			for _,Entity in pairs( ents.GetAll() ) do
				local Time = Entity:WPP_AutoRemoveTime()
				if ( Time ) and ( Time <= 0 ) then
					Entity:Remove()
					local Owner = Entity:WPP_Owner()
					if ( Owner ) then
						Owner:PrintMessage( HUD_PRINTNOTIFY, "You item has been automaticly removed.")
					end
				end
			end
		end
	end)
	
end